<template>
  <div class="org-tree-node" :class="{ 'is-root': isRoot, 'is-leaf': !hasChildren }">
    <div class="org-tree-node-content">
      <div class="org-tree-node-inner">{{ data.label }}</div>
      <div v-if="hasChildren" class="org-tree-node-btn" @click="data.expend = !data.expend" />
    </div>
    <div v-if="hasChildren" v-show="data.expend === undefined || !data.expend" class="org-tree-node-children">
      <org-tree-node
        v-for="node in data.children"
        :key="node.id"
        :data="node"
        :is-root="false"
      />
    </div>
  </div>
</template>

<script setup>
const props = defineProps({
  data: {
    type: Object,
    default() {
      return {}
    }
  },
  isRoot: {
    type: Boolean,
    default: true
  }
})

const hasChildren = computed(() => Array.isArray(props.data.children) && props.data.children?.length)
</script>

<style scoped lang="scss">
.org-tree-node, .org-tree-node-children {
  position: relative;
  margin: 0;
  padding: 0;
  list-style-type: none;
}
.org-tree-node.is-root {
  //display: flex !important;
  //align-items: center;
  //flex-direction: column;
  //padding-top: 0;
}
.org-tree-node {
  display: table-cell;
  padding-top: 20px;
  vertical-align: top;
  .org-tree-node-content {
    position: relative;
    user-select: none;
    display: inline-block;
    .org-tree-node-inner {
      padding: 10px 15px;
      text-align: center;
      border-radius: 3px;
      box-shadow: 0 1px 5px rgba(0, 0, 0, .15);
    }
    .org-tree-node-btn {
      position: absolute;
      top: 100%;
      left: 50%;
      width: 20px;
      height: 20px;
      z-index: 10;
      margin-left: -11px;
      margin-top: 9px;
      background-color: #fff;
      border: 1px solid #ccc;
      border-radius: 50%;
      box-shadow: 0 0 2px rgba(0, 0, 0, .15);
      cursor: pointer;
      transition: all .35s ease;

      &:hover {
        background-color: #e7e8e9;
        transform: scale(1.15);
      }

      &:before, &:after {
        content: '';
        position: absolute;
      }

      &:before {
        top: 50%;
        left: 4px;
        right: 4px;
        height: 0;
        border-top: 1px solid #ccc;
      }

      &:after {
        top: 4px;
        left: 50%;
        bottom: 4px;
        width: 0;
        border-left: 1px solid #ccc;
      }

      &.expanded:after {
        border: none;
      }
    }
  }
  &.is-leaf {
    padding-left: 15px;
    padding-right: 15px;
  }
  &:before, &:after {
    content: "";
    position: absolute;
    top: 0;
    left: 0;
    width: 50%;
    height: 19px;
  }
  &:after {
    left: 50%;
    border-left: 1px solid #ddd;
  }
  &:not(:first-child):before, &:not(:last-child):after {
    border-top: 1px solid #ddd;
  }
  &.is-root:before, &.is-root:after {
    display: none;
  }
}
.org-tree-node-children {
  display: table;
  text-align: center;
  padding-top: 20px;
  width: 100%;
  &:before {
    content: "";
    position: absolute;
    top: 0;
    left: 50%;
    width: 0;
    height: 20px;
    border-left: 1px solid #ddd;
  }
  &:after {
    content: "";
    display: table;
    clear: both;
  }
}
</style>